package cn.npe.dynamic.thread.pool.sdk.config.postprocessor;

import org.apache.commons.lang.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * packageName cn.npe.dynamic.thread.pool.sdk.config.postprocessor
 *
 * @author zhjx
 * @className DtpEnviromentPostProcessor (此处以class为例)
 * @date 2024/5/24
 * @description TODO
 */
public class DtpEnvironmentPostProcessor implements EnvironmentPostProcessor {
    private final YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String isEnabled = environment.getProperty("spring.dtp.registry.enabled");
        boolean autoconfigure = environment.containsProperty("spring.autoconfigure.exclude");
        String type = environment.getProperty("spring.dtp.registry.type");

        if(StringUtils.isEmpty(isEnabled)||"false".equals(isEnabled)){
            environment.getPropertySources().addLast(new MyPropertySource());
        }
        if("true".equals(isEnabled)&&!"redis".equals(type)){
            environment.getPropertySources().addLast(new MyPropertySource());
        }
    }

    private static class MyPropertySource extends MapPropertySource {

        public MyPropertySource() {
            super("myPropertySource", Collections.singletonMap("spring.autoconfigure.exclude", "org.redisson.spring.starter.RedissonAutoConfigurationV2"));
        }
    }


}